var vm = new Vue({
    el: '#app',
     // 修改vue模板符号，防止与django冲突
    data: {
        host: host,
        username: sessionStorage.username || localStorage.username,
        user_id: sessionStorage.user_id || localStorage.user_id,
        avatar_url: sessionStorage.avatar_url || localStorage.avatar_url,
        token: sessionStorage.token || localStorage.token,
        nick_name: sessionStorage.nick_name || localStorage.nick_name,
        page: 1, // 当前页数
        page_size: 10, // 每页数量
        count: 0,  // 总数量

        host,
        user:[],
        hide:false,
        hide1:false,
        hide2:false,
        hide3:false,
        flag1:true,

        nick_name:"",
        school_name:'',
        professional:'',
        company:'',
        profession:'',
        web_site:'',
        city:'',
        introduce:'',

        dynamics: [] // 个人动态

    },
    computed: {
        total_page: function () {  // 总页数
            return Math.ceil(this.count / this.page_size);
        },
        next: function () {  // 下一页
            if (this.page >= this.total_page) {
                return 0;
            } else {
                return this.page + 1;
            }
        },
        previous: function () {  // 上一页
            if (this.page <= 0) {
                return 0;
            } else {
                return this.page - 1;
            }
        },
        page_nums: function () {  // 页码
            // 分页页数显示计算
            // 1.如果总页数<=5
            // 2.如果当前页是前3页
            // 3.如果当前页是后3页,
            // 4.既不是前3页，也不是后3页
            var nums = [];
            if (this.total_page <= 5) {
                for (var i = 1; i <= this.total_page; i++) {
                    nums.push(i);
                }
            } else if (this.page <= 3) {
                nums = [1, 2, 3, 4, 5];
            } else if (this.total_page - this.page <= 2) {
                for (var i = this.total_page; i > this.total_page - 5; i--) {
                    nums.push(i);
                }
            } else {
                for (var i = this.page - 2; i < this.page + 3; i++) {
                    nums.push(i);
                }
            }
            return nums;
        }
    },
    mounted: function () {
        this.get_dynamics()
        this.locationStart()
    },

    methods: {
        logout: function () {
            sessionStorage.clear();
            localStorage.clear();
            location.href = './person-loginsign.html';
        },
        // 获取url路径参数
        get_query_string: function (name) {
            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return decodeURI(r[2]);
            }
            return null;
        },

        // 点击页数
        on_page: function (num) {
            if (num != this.page) {
                this.page = num;
                this.get_dynamics();
            }
        },
        // 点击排序
        on_sort: function (ordering) {
            if (ordering != this.ordering) {
                this.page = 1;
                this.ordering = ordering;
                this.get_dynamics();
            }
        },

        // 获取个人动态数据
        showCont: function (flag) {
                if (this.user_id && this.token) {
                    if (flag == 0) {
                        vm.hide = false
                        axios.patch(this.host + '/accounts/'+this.user_id+'/', {
                                city: this.city
                            },
                            {
                                headers: {
                                    'Authorization': 'JWT ' + this.token

                                },
                                responseType: 'json',
                                withCredentials:true
                            })
                            .then(response => {

                                this.city=response.data.city
                                alert('保存成功')
                            })
                            .catch(error => {
                                if (error.response.status == 400) {
                                    if ('non_field_errors' in error.response.data) {
                                        this.error_sms_code_message = error.response.data.non_field_errors[0];
                                    } else {
                                        this.error_sms_code_message = '数据有误';
                                    }
                                    this.error_sms_code = true;
                                } else {
                                    console.log(error.response.data);
                                }
                            })
                    } else if (flag == 1) {
                        vm.hide1 = false
                         axios.patch(this.host + '/school/'+this.user_id+'/', {
                             school_name:this.school_name,
                             professional:this.professional,
                            },
                            {
                                headers: {
                                    'Authorization': 'JWT ' + this.token

                                },
                                responseType: 'json',
                                withCredentials:true
                            })
                            .then(response => {
                                alert('保存成功')
                            })
                            .catch(error => {
                                console.log(response)
                                if (error.response.status == 400) {

                                    if ('non_field_errors' in error.response.data) {
                                        this.error_sms_code_message = error.response.data.non_field_errors[0];
                                    } else {
                                        this.error_sms_code_message = '数据有误';
                                    }
                                    this.error_sms_code = true;
                                } else {

                                    console.log(error.response.data);
                                }
                            })
                    } else if (flag == 2) {
                        vm.hide2 = false
                        axios.patch(this.host + '/company/'+this.user_id+'/', {
                            company: this.company,
                            profession:this.profession,
                            },
                            {
                                headers: {
                                    'Authorization': 'JWT ' + this.token

                                },
                                responseType: 'json',
                                withCredentials:true
                            })
                            .then(response => {
                                alert('保存成功')
                            })
                            .catch(error => {
                                 alert('参数错误')
                                if (error.response.status == 400) {
                                    if ('non_field_errors' in error.response.data) {
                                        this.error_sms_code_message = error.response.data.non_field_errors[0];
                                    } else {
                                        this.error_sms_code_message = '数据有误';
                                    }
                                    this.error_sms_code = true;
                                } else {

                                    console.log(error.response.data);
                                }
                            })
                    } else if (flag == 3) {
                        vm.hide3 = false
                        axios.patch(this.host + '/accounts/'+this.user_id+"/", {
                               web_site: this.web_site
                            },
                            {
                                headers: {
                                    'Authorization': 'JWT ' + this.token

                                },
                                responseType: 'json',
                                withCredentials:true
                            })
                            .then(response => {
                                alert('保存成功')
                            })
                            .catch(error => {
                                if (error.response.status == 400) {
                                    if ('non_field_errors' in error.response.data) {
                                        this.error_sms_code_message = error.response.data.non_field_errors[0];
                                    } else {
                                        this.error_sms_code_message = '数据有误';
                                    }
                                    this.error_sms_code = true;
                                } else {

                                    console.log(error.response.data);
                                }
                            })
                    }
                }

            },
        mytest:function (a) {
                if (this.user_id && this.token) {
                    if(a==0){
                    console.log("chenggong")
                        　vm.flag1=true
                         axios.patch(this.host + '/accounts/'+this.user_id+'/', {
                                introduce: this.introduce
                            },
                            {
                                headers: {
                                    'Authorization': 'JWT ' + this.token

                                },
                                responseType: 'json',
                                withCredentials:true
                            })
                            .then(response => {
                                alert('保存成功')
                            })
                            .catch(error => {
                                if (error.response.status == 400) {

                                    if ('non_field_errors' in error.response.data) {
                                        this.error_sms_code_message = error.response.data.non_field_errors[0];
                                    } else {
                                        this.error_sms_code_message = '数据有误';
                                    }
                                    this.error_sms_code = true;
                                } else {
                                    console.log(error.response.data);
                                }
                            })
                    }else if(a==1){
                        　vm.flag2=true
                         axios.patch(this.host + '/edit/'+this.user_id+'/', {
                                avatar_url: this.avatar_url,
                                username: this.username,
                                birthday: this.birthday,
                                gender: this.num,
                                web_site: this.web_site,
                                mobile: this.mobile,
                                email: this.email,
                                city: this.city,
                                hometown: this.hometown,
                            },
                            {
                                headers: {
                                    'Authorization': 'JWT ' + this.token

                                },
                                responseType: 'json',
                                withCredentials:true
                            })
                            .then(response => {
                                this.totalSite()
                                console.log(response.data)
                                alert('保存成功')
                            })
                            .catch(error => {
                                if (error.response.status == 400) {

                                    if ('non_field_errors' in error.response.data) {
                                        this.error_sms_code_message = error.response.data.non_field_errors[0];
                                    } else {
                                        this.error_sms_code_message = '数据有误';
                                    }
                                    this.error_sms_code = true;
                                } else {
                                    console.log(error.response.data);
                                }
                            })
                    }else if(a==2) {
                        vm.flag3 = true
                        axios.patch(this.host + '/accounts/' + this.user_id + '/', {
                                technology:this.technology
                            },
                            {
                                headers: {
                                    'Authorization': 'JWT ' + this.token

                                },
                                responseType: 'json',
                                withCredentials: true
                            })
                            .then(response => {
                                this.totalSite()
                                console.log(response.data)
                                alert('保存成功')
                            })
                            .catch(error => {
                                if (error.response.status == 400) {

                                    if ('non_field_errors' in error.response.data) {
                                        this.error_sms_code_message = error.response.data.non_field_errors[0];
                                    } else {
                                        this.error_sms_code_message = '数据有误';
                                    }
                                    this.error_sms_code = true;
                                } else {
                                    console.log(error.response.data);
                                }
                            })
                    }else if(a==3){
                    　
                    }else if(a==4){
                        　vm.flag6=true
                         axios.post(this.host + '/work/'+this.user_id+'/', {
                                background:this.background,
                                edu_technology:this. edu_technology,
                                achievements:this.achievements
                            },
                            {
                                headers: {
                                    'Authorization': 'JWT ' + this.token

                                },
                                responseType: 'json',
                                withCredentials:true
                            })
                            .then(response => {
                                console.log(response.data)
                                alert('保存成功')
                            })
                            .catch(error => {
                                if (error.response.status == 400) {

                                    if ('non_field_errors' in error.response.data) {
                                        this.error_sms_code_message = error.response.data.non_field_errors[0];
                                    } else {
                                        this.error_sms_code_message = '数据有误';
                                    }
                                    this.error_sms_code = true;
                                } else {
                                    console.log(error.response.data);
                                }
                            })
                    }
                }

            },
        locationStart:function () {
                if (this.user_id && this.token) {
                axios.get(this.host + '/user/', {
                        // 向后端传递JWT token的方法
                        headers: {
                            'Authorization': 'JWT ' + this.token
                        },
                        responseType: 'json',
                    })
                    .then(response => {
                        // 加载用户数据

                        this.user = response.data
                        this.nick_name = this.user.nick_name,
                        this.school_name = this.user.school.school_name,
                        this.professional = this.user.school.professional,
                        this.company = this.user.company.company,
                        this.profession = this.user.company.profession,
                        this.web_site = this.user.detail.web_site,
                        this.city = this.user.detail.city
                        this.introduce=this.user.detail.introduce

                    })
                    .catch(error => {

                        if (error.response.status==401 || error.response.status==403) {
                            location.href = '/person-loginsign.html?next=/person-account.html';
                        }
                    });
            } else {
                location.href = '/person-loginsign.html?next=/person-account.html';
            }
            },
        get_dynamics:function () {
            axios.get(this.host + '/user/dynamics/', {
                params: {
                    page: this.page,
                    page_size: this.page_size,
                },
                headers: {
                    // 后台接收的JWT对格式有要求,'JWT'开头,空一格拼接字符串
                    'Authorization': 'JWT ' + this.token
                },
                responseType: 'json'
            })
                .then(response => {
                    // 保存总数量,用于计算页数
                    this.count = response.data.count;
                    this.dynamics = response.data.results;
                    this.nowInS = new Date().getTime() / 1000;  // getTime() 取的是毫秒数
                    // let mytime=this.dynamics.create_time.Date().getHours()

                    this.deadlineInS = new Date().getTime() / 1000
                    // console.log(this.dynamics.create_time - this.deadlineInS)
                    // console.log(this.dynamics)
                    // console.log(this.nowInS)
                    // console.log(this.deadlineInS)
                })
                .catch(error => {
                    console.log(error.response.data);
                })
        }
    }
});